from masoniteorm.relationships import belongs_to
from masoniteorm.schema import Schema

from application.config.database import DB
from application.config import appSettings
from ..base import BaseModel


class CodeLog(BaseModel):
    __table__ = appSettings.db_prefix + 'code_logs'

    @belongs_to('code', 'code')
    def brand(self):
        from .Company import Company
        return Company


class CodeCollection(BaseModel):
    __table__ = appSettings.db_prefix + 'code_collections'

    @belongs_to('scan_id', 'id')
    def codelog(self):
        return CodeLog

    @belongs_to('brand_id', 'brand_id')
    def brand(self):
        from .Company import Company
        return Company

    @staticmethod
    def create_table():
        """创建表单收集表

            CREATE TABLE `pps_qiema_cn`.`qem_fw_forms` (
                `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
                `customer_id` int(11) unsigned DEFAULT '0' COMMENT '品牌商',
                `scan_id` int(11) unsigned DEFAULT '0' COMMENT '扫码 ID',
                `batch` varchar(50) DEFAULT NULL COMMENT '批次',
                `code` varchar(60) DEFAULT NULL COMMENT '流水码',
                `fwcode` varchar(60) DEFAULT NULL COMMENT '防伪码',
                `name` varchar(250) DEFAULT NULL COMMENT '姓名',
                `phone` varchar(250) DEFAULT NULL COMMENT '手机号码',
                `wechat` varchar(250) DEFAULT NULL COMMENT '微信/QQ/微博/其它',
                `email` varchar(250) DEFAULT NULL COMMENT '邮箱',
                `ext1` varchar(250) DEFAULT NULL COMMENT '扩展 1',
                `ext2` varchar(250) DEFAULT NULL COMMENT '扩展 2',
                `ext3` varchar(250) DEFAULT NULL COMMENT '扩展 3',
                `ext4` varchar(250) DEFAULT NULL COMMENT '扩展 4',
                `ext5` varchar(250) DEFAULT NULL COMMENT '扩展 5',
                `ext6` varchar(250) DEFAULT NULL COMMENT '扩展 6',
                `form_content` longtext COMMENT '表单提交数据(json)',
                `created_time` int(11) unsigned DEFAULT '0' COMMENT '创建时间',
                `updated_time` int(11) unsigned DEFAULT '0' COMMENT '更新时间',
            PRIMARY KEY (`id`),
            KEY `scan_id` (`scan_id`),
            KEY `batch` (`batch`),
            KEY `code` (`code`),
            KEY `fwcode` (`fwcode`),
            KEY `customer_id` (`customer_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='防伪查询表单收集表';
        """

        table_name = __class__.__table__
        # 动态创建表
        mSchema = Schema(connection=__class__.__connection__, connection_details=DB.get_connection_details())
        # 如果没有表则创建
        if not mSchema.has_table(table_name):
            with mSchema.create(table_name) as table:
                table.big_increments('id').primary()
                table.integer('brand_id', 11, True).index().comment('品牌ID')
                table.string('brand_code', 32, True).index().comment('品牌旧 Code')
                table.integer('scan_id', 11, True).index().comment('扫码 ID')
                table.string('batch_sn', 50).index().comment('批次')
                table.string('serial_number', 50).nullable().index().comment('流水号码')
                table.string('security_code', 50).index().comment('防伪码')
                table.string('name', 250).nullable().comment('姓名')
                table.string('phone', 250).nullable().comment('手机号码')
                table.string('wechat', 250).nullable().comment('微信/QQ/微博/其它')
                table.string('email', 250).nullable().comment('邮箱')
                table.string('ext1', 250).nullable().comment('扩展 1')
                table.string('ext2', 250).nullable().comment('扩展 2')
                table.string('ext3', 250).nullable().comment('扩展 3')
                table.string('ext4', 250).nullable().comment('扩展 4')
                table.string('ext5', 250).nullable().comment('扩展 5')
                table.string('ext6', 250).nullable().comment('扩展 6')
                table.text('form_content').nullable().comment('表单提交数据(json)')
                # created_at/updated_at
                table.timestamp('created_at', True, True)
                table.timestamp('updated_at', True, True)


__all__ = [
    'CodeLog',
    'CodeCollection',
]
